package com.lianboke.link.ad.utils;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.provider.SyncStateContract;

import com.lianboke.link.ad.common.CommonParams;

import java.util.Calendar;

import static android.app.AlarmManager.RTC_WAKEUP;


/**
 * 定时器工具管理
 * Created by admin on 2018/1/22.
 */

public class AlarmManagerUtils{

    /**
     * 设置闹钟
     */
    public static void setAlarmParams(Context appContext,String action,int requestCode,int hourOfDay,int minute){
        Calendar calendar=Calendar.getInstance();
        calendar.set(Calendar.MINUTE, minute);//分钟
        calendar.set(Calendar.SECOND, 0);//秒
        calendar.set(Calendar.MILLISECOND, 0);//毫秒
        calendar.set(Calendar.HOUR_OF_DAY,hourOfDay);//小时

        long triggerAtMillis = calendar.getTimeInMillis();
        long intervalMillis = 1000*60*60*24;//24小时间隔
        AlarmManager aManager = (AlarmManager)appContext.getSystemService(Service.ALARM_SERVICE);
        Intent intent=new Intent(action);//
        PendingIntent pi= PendingIntent.getBroadcast(appContext, requestCode, intent,PendingIntent.FLAG_UPDATE_CURRENT);
        aManager.setRepeating(RTC_WAKEUP,triggerAtMillis,intervalMillis,pi);
    }

    /**
     * 取消闹钟
     */
    public static void cancleAlarmParams(Context appContext,String action ,int requestCode){
        AlarmManager aManager = (AlarmManager)appContext.getSystemService(Service.ALARM_SERVICE);
        // 取消AlarmManager的定时服务
        Intent intent=new Intent(action);// 和设定闹钟时的action要一样
        // 这里PendingIntent的requestCode、intent和flag要和设定闹钟时一样
        PendingIntent pi= PendingIntent.getBroadcast(appContext, requestCode, intent,PendingIntent.FLAG_UPDATE_CURRENT);
        aManager.cancel(pi);
    }


}
